package com.menggudf.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.menggudf.entity.AdminPermissions;
import com.menggudf.entity.SystemAdmin;

@Repository
public class AdminPermissionDao {
    
    private static final Logger LOGGER = LoggerFactory
            .getLogger(AdminPermissionDao.class);

    @PersistenceContext(unitName = "medicalUnit")
    private EntityManager entityManager;

    public void persist(AdminPermissions transientInstance) {
        try {
            entityManager.persist(transientInstance);
        } catch (RuntimeException re) {
            LOGGER.error("persist failed", re);
            throw re;
        }
    }

    public void remove(AdminPermissions persistentInstance) {
        try {
            entityManager.remove(persistentInstance);
        } catch (RuntimeException re) {
            LOGGER.error("remove failed", re);
            throw re;
        }
    }

    public AdminPermissions merge(AdminPermissions detachedInstance) {
        LOGGER.debug("merging AdminPermissions instance");
        try {
            return entityManager.merge(detachedInstance);
        } catch (RuntimeException re) {
            LOGGER.error("merge failed", re);
            throw re;
        }
    }

    public AdminPermissions findById(Long id) {
        try {
            return entityManager.find(AdminPermissions.class, id);
        } catch (RuntimeException re) {
            LOGGER.error("get failed", re);
            throw re;
        }
    }

    public boolean hasOper(String adminCode, String oper) {
        try {
            Query query = entityManager.createQuery("from AdminPermissions where adminCode=?1 and permissionCode=?2")
                    .setParameter(1, adminCode).setParameter(2, oper);
            List<AdminPermissions> list = query.getResultList();
            if(list!=null && list.size()>0){
                return true;
            }else{
                return false;
            }
        } catch (Exception e) {
            LOGGER.error("hasOper fail",e);
            throw e;
        }
    }

    public List<AdminPermissions> getAdminPerssionByCode(String adminCode) {
        try {
            Query query = entityManager.createQuery("from AdminPermissions where adminCode=?1")
                    .setParameter(1, adminCode);
            return query.getResultList();
        } catch (Exception e) {
            LOGGER.error("getAdminPerssionByCode fail",e);
            throw e;
        }
    }

    public AdminPermissions getAdminPerssionByCodeAndId(String adminCode, Integer id) {
        try {
            Query query = entityManager.createQuery("from AdminPermissions where adminCode=?1 and permissionid=?2")
                    .setParameter(1, adminCode).setParameter(2, id);
            List<AdminPermissions> list = query.getResultList();
            if(list!=null && list.size()>0){
                return list.get(0);
            }else{
                return null;
            }
        } catch (Exception e) {
            LOGGER.error("getAdminPerssionByCodeAndId fail",e);
            throw e;
        }
    }

}
